import type { Plugin } from 'vite';
import { extname } from 'path';
import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
import legacy from '@vitejs/plugin-legacy';
import { autoRegistryComponents } from './component';
import { AutoImportDeps } from './autoImport';
import { configVisualizerConfig } from './visualizer';
import { configCompressPlugin } from './compress';
import esbuild from 'rollup-plugin-esbuild';
import eslintPlugin from 'vite-plugin-eslint';

export function createVitePlugins(isBuild: boolean) {
  const vitePlugins: (Plugin | Plugin[])[] = [
    // vue支持
    vue(),
    // JSX支持
    vueJsx(),
    {
      ...esbuild({
        target: 'chrome70',
        include: /\.(vue|ts)$/, // 如有需要可以在这里加 js ts 之类的其他后缀
        loaders: {
          '.vue': 'js',
          '.ts': 'js',
        },
      }),
      enforce: 'post',
    } as any,
  ];
  // 自动按需引入组件
  isBuild && false && vitePlugins.push(autoRegistryComponents());
  // 自动按需引入依赖
  isBuild && false && vitePlugins.push(AutoImportDeps());
  // @vitejs/plugin-legacy 兼容老版本浏览器
  vitePlugins.push(
    legacy({
      targets: ['chrome 49'],
      additionalLegacyPolyfills: ['regenerator-runtime/runtime'],
    }),
  );

  // rollup-plugin-gzip   gzip 压缩
  isBuild && vitePlugins.push(configCompressPlugin());

  // rollup-plugin-visualizer
  vitePlugins.push(configVisualizerConfig());

  return vitePlugins;
}
